<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="../frame/header.jsp"></jsp:include>

<title>菜品管理</title>

<style>
.pagination .active a {
	/*background-color: #CC0041;
	color:white;*/
}
div.in-line{
	margin-top:5px;
}
div.in-line label{
	width:70px;
	text-align:right;
}
</style>

</head>

<body>

	<div class="container-fluid">
		<div id="market" class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">商家管理</a> <span class="divider">/</span></li>
					<li><a href="#">菜品管理</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-th"></i> 商家菜品列表
					</h2>

					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<div id="search-form">
						<form id="searchForm" method="post" class="form-search span12" action="<c:url value="/admin/book/search_food"/>">
							<input type="hidden" name="pageNum" id="pageNum" value="${searchForm.pageNum }">
							<input type="hidden" name="foodId" id="foodId" value="0">
							
							<div class="in-line">
								<label>菜品名称：</label> 
								<input type="text" name="search" id="searchText" value="${searchForm.search }" 
								class="input-medium search-query" placeholder="">
								<label>选择商家：</label> 
								<select id="sellerId" class="input-medium" name="sellerId"
									check-type="required" required-message="请选择所属商家">
									<option value="0">全部</option>
									<c:forEach var="seller" items="${sellers }">
										<option value="${seller.sellerId }"
											<c:if test="${searchForm.sellerId == seller.sellerId}">selected</c:if>>
											${seller.sellerName}</option>
									</c:forEach>
								</select>
								<label>菜单分类：</label> 
								<select data-placeholder="全部" id="menus_0"
									name="menuId" data-rel="chosen" class="input-medium">
									<option value="0">全部</option>

								</select>
								<button type="button" id="searchBtn" class="btn">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
							</div>
							
							<button type="button" id="newBtn" class="btn btn-danger pull-right">添加菜品</button>
						</form>

					</div>
					<table id="userInfo" class="table table-striped table-bordered ">
						<thead>
							<tr>
								<th width="30">序号</th>
								<th >菜品名称</th>
								<th >商家名称</th>
								<th >菜单分类</th>
								<th >价位</th>
								<th >折扣</th>
								<th width="50">操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach var="food" items="${pageModel.result }" varStatus="status">
								<tr>
									<td>${status.index+1 }</td>
									<td>${food.foodName }</td>
									<td>${food.sellerName } </td>
									<td>${food.menuName } </td>
									<td>${food.price }</td>
									<td>${food.rebate }折</td>
									
									<td><a href="javascript:edit('${food.foodId }')"><i
											class="icon-edit"></i></a> &nbsp;&nbsp; <a
										href="javascript:del('${food.foodId }')"><i class="icon-minus"></i></a></td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					
					<div id="mypager" class="mypager"></div>
				</div>
			</div>
		</div>
		<!--/fluid-row-->

	</div>
	<!--/.fluid-container-->

	<jsp:include page="../frame/scripts.jsp"></jsp:include>
	<script type="text/javascript">
	
	var pageNum = "${pageModel.pageNum}";
	var totalPage = "${pageModel.totalPage}";
	var numPerPage = "${pageModel.numPerPage}";
	
	$(document).ready(function(){
		
		if($('#sellerId').val() == null){
			alert("商家不存在，请首先添加商家信息！");
			window.location.href = "<c:url value="/admin/book/seller"/>";
		}else{
			
			$('#sellerId').change(function(){
				var sellerId = $(this).val();
				$.ajax({
					url:'<c:url value="/admin/book/menu/tree" />',
					dataType:'json',
					data:{sellerId:sellerId},
					success:function(data){
						$("#menus_0").html('<option value="0">全部</option>');
						//搜索表单初始化版块下拉框
						createTree(data,"menus");
						//设置选中
						$("#menus_0").val("${searchForm.menuId}");
						
						
					}
				})
			}).trigger('change');
		}
		
		$("#newBtn").click(function(){
			//跳转至新增页面
			var sellerId = $('#sellerId').val();
			window.location.href = '<c:url value="/admin/book/addFood" />';
		})
		
		$('#mypager').bootstrapPaginator({
			currentPage:pageNum,
			totalPages:totalPage,
			numberOfPages:numPerPage,
			onPageClicked:function(event, originalEvent, type,page){
				$("input[name='pageNum']").val(page);
				$("#searchForm").submit();
			}
		});
		
		
		$('#searchBtn').click(function(){
			$("#searchForm").submit();
		})
		
		
	})
	
	function edit(foodId){
		window.location.href = '<c:url value="/admin/book/editFood"/>?foodId='+foodId;
	}
	
	function del(foodId){
		if(confirm("确定要删除吗？")){
			$("input[name='foodId']").val(foodId);
			$("#searchForm").submit();
		}
	}
	
	function createTree(data,tagId){
		$.each(data,function(i,item){
			//生成节点
			createNode(item,tagId);
			if(item.children.length > 0){
				//生成子节点
				createTree(item.children,tagId);
			}
		})
	}
	
	function createNode(item,tagId){
		var $parent_select = $("#"+tagId+"_"+item.menuParent);
		var str = '';
		if(item.children.length > 0){
			str += '<optgroup id="'+tagId+'_'+item.menuId+'" label="'+item.menuName+'">';
			str += '</optgroup>';
		}else{
			str += '<option value='+item.menuId+'>'+item.menuName+'</option>';
		}
		
		$parent_select.append(str);
	}
	</script>
</body>
</html>
